package com.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;


import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.news;
import com.yunhe.bean.page;
import com.yunhe.inter.newsInter;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * Servlet implementation class selectNew
 */
@WebServlet("/selectNew")
public class selectNew extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public selectNew() {
    	
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
  //其他链接方法，跳转页面
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
/*	弃用服务编码	
 * 		//确认接收编码
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//建立数据库会话链接
		SqlSession selectSession=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")).openSession();
		//建立接口映射
		newsInter mapper=selectSession.getMapper(newsInter.class);
		//创建页码对象
		page p=new page();
		p.setCurrentPage(1);
		if(request.getParameter("CurrentPage")!=null && request.getParameter("CurrentPage")!=""){
			p.setCurrentPage(Integer.parseInt(request.getParameter("CurrentPage")));
		}
		System.out.println(p.getCurrentPage()+" "+p.getIndexnum()+" "+p.getEndnum());
		//获取新闻信息
		ArrayList<news> newsList=mapper.selectNews(p);
		//获取总新闻数
		int totalNews=mapper.totalNews();
		p.setTotalNews(totalNews);
		//关闭会话
		selectSession.close();
		//封存信息
		request.setAttribute("newsList", newsList);
		request.setAttribute("Page", p);
		//跳转页面
		request.getRequestDispatcher("manage/news.jsp").forward(request, response);*/
		response.sendRedirect("manage/news.jsp");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	//Ajax获取信息方法
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//确认接收编码
				response.setCharacterEncoding("UTF-8");
				request.setCharacterEncoding("UTF-8");
				//建立输出流
				PrintWriter out=response.getWriter();
				//获取sqlSession
				SqlSession sqlSession = SqlSessionFactoryUtil.openSqlSession();
				//建立接口映射
				newsInter mapper=sqlSession.getMapper(newsInter.class);
				//创建分页对象
				page p=new page();
				p.setCurrentPage(1);
				if(request.getParameter("CurrentPage")!=null && request.getParameter("CurrentPage")!=""){
					p.setCurrentPage(Integer.parseInt(request.getParameter("CurrentPage")));
				}
				//获取新闻信息
				ArrayList<news> newsList=mapper.selectNews(p);
				//获取总新闻数
				int totalNews=mapper.totalNews();
				p.setTotalNews(totalNews);
				sqlSession.close();
				//建立Map集合填入数据
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("newslist", newsList);
				map.put("page", p);
				//转换Json格式
				JSONObject job=JSONObject.fromObject(map);
				System.out.println(job.toString());
				out.write(job.toString());
	}

}
